package cn.org.timespace.easyframe.dao.permission.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.easyframe.action.permission.EfUserLoginLogModel;
import cn.org.timespace.easyframe.dao.EfBaseDAO;
import cn.org.timespace.easyframe.dao.permission.EfUserLoginLogDao;
import cn.org.timespace.easyframe.model.permission.EfUserLoginLog;
import cn.org.timespace.easyframe.util.pager.Pager;

public class EfUserLoginLogDaoImpl extends EfBaseDAO implements
		EfUserLoginLogDao {

	public List<EfUserLoginLog> findByCondition(EfUserLoginLogModel model) {
		log.debug("finding all EfUserLoginLog instances");
		
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from EfUserLoginLog a where 1=1 ");
			
			if(!StringUtils.isEmpty(model.getLogId())){
				queryString.append(" and a.logId = '"+model.getLogId().trim()+"' ");
			} 
			
			if(!StringUtils.isEmpty(model.getUserAccount())){
				queryString.append(" and a.efUser.userAccount = '"+model.getUserAccount().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getUserName())){
				queryString.append(" and a.efUser.userName like '%"+model.getUserName().trim()+"%' ");
			}
			
			if(!StringUtils.isEmpty(model.getLogonDateTimeStart()) && !StringUtils.isEmpty(model.getLogonDateTimeEnd())){
				queryString.append(" and a.logonDatetime >= :logonDatetimeStart ");
				queryString.append(" and a.logonDatetime <= :logonDatetimeEnd ");
			}else if(!StringUtils.isEmpty(model.getLogonDateTimeStart())){
				queryString.append(" and a.logonDatetime >= :logonDatetimeStart ");
			}else if(!StringUtils.isEmpty(model.getLogonDateTimeEnd())){
				queryString.append(" and a.logonDatetime <= :logonDatetimeEnd ");
			}
			
			queryString.append(" order by logonDatetime desc ");
			
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			
			if(!StringUtils.isEmpty(model.getLogonDateTimeStart()) && !StringUtils.isEmpty(model.getLogonDateTimeEnd())){
				query.setDate("logonDatetimeStart", java.sql.Date.valueOf(model.getLogonDateTimeStart()));
				query.setDate("logonDatetimeEnd", java.sql.Date.valueOf(model.getLogonDateTimeEnd()));
			}else if(!StringUtils.isEmpty(model.getLogonDateTimeStart())){
				query.setDate("logonDatetimeStart", java.sql.Date.valueOf(model.getLogonDateTimeStart()));
			}else if(!StringUtils.isEmpty(model.getLogonDateTimeEnd())){
				query.setDate("logonDatetimeEnd", java.sql.Date.valueOf(model.getLogonDateTimeEnd()));
			}			
			
			return query.list();
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}
	
	public List<Object> findByCondition(EfUserLoginLogModel model,Pager p) {
		log.debug("finding all EfUserLoginLog instances");
		
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from EfUserLoginLog a where 1=1 ");
			
			if(!StringUtils.isEmpty(model.getLogId())){
				queryString.append(" and a.logId = '"+model.getLogId().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getUserAccount())){
				queryString.append(" and a.efUser.userAccount = '"+model.getUserAccount().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getUserName())){
				queryString.append(" and a.efUser.userName like '%"+model.getUserName().trim()+"%' ");
			}
			
			if(!StringUtils.isEmpty(model.getLogonDateTimeStart()) && !StringUtils.isEmpty(model.getLogonDateTimeEnd())){
				queryString.append(" and a.logonDatetime >= :logonDatetimeStart ");
				queryString.append(" and a.logonDatetime <= :logonDatetimeEnd ");
			}else if(!StringUtils.isEmpty(model.getLogonDateTimeStart())){
				queryString.append(" and a.logonDatetime >= :logonDatetimeStart ");
			}else if(!StringUtils.isEmpty(model.getLogonDateTimeEnd())){
				queryString.append(" and a.logonDatetime <= :logonDatetimeEnd ");
			}
			
			queryString.append(" order by logonDatetime desc ");
			
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			System.out.println(queryString.toString());
			if(!StringUtils.isEmpty(model.getLogonDateTimeStart()) && !StringUtils.isEmpty(model.getLogonDateTimeEnd())){
				query.setDate("logonDatetimeStart", java.sql.Date.valueOf(model.getLogonDateTimeStart()));
				query.setDate("logonDatetimeEnd", java.sql.Date.valueOf(model.getLogonDateTimeEnd()));
			}else if(!StringUtils.isEmpty(model.getLogonDateTimeStart())){
				query.setDate("logonDatetimeStart", java.sql.Date.valueOf(model.getLogonDateTimeStart()));
			}else if(!StringUtils.isEmpty(model.getLogonDateTimeEnd())){
				query.setDate("logonDatetimeEnd", java.sql.Date.valueOf(model.getLogonDateTimeEnd()));
			}			
			
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			
			setQueryPager(query, p);
			resList.add(query.list());
			
			return resList;
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}

	public void save(EfUserLoginLog transientInstance) {
		log.debug("saving EfUserLoginLog instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(EfUserLoginLog entity) {
		log.debug("updating EfUserLoginLog instance");
		try {
			getHibernateTemplate().update(entity);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	

}
